<?php

namespace App\Admin\Controllers;

use App\Models\MiniProgramMainTab;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Support\Arr;

class MiniProgramMainTabController extends AdminController
{
    public function index(Content $content)
    {
        $slug = request()->input('mini_type');

        $tab = Tab::make();
        $tab->addLink('首页模块管理', admin_route('mini_program_special.index', ['mini_type' => $slug]));
        $tab->addLink('底部菜单管理', admin_route('mini_program_tab.index', ['mini_type' => $slug]));
        $tab->add('首页导航', $this->grid(), true);
        $tab->addLink('小程序导航', admin_route('mini_main_nav.index', ['mini_type' => $slug]));
        $tab->addLink('轮播图设置', admin_route('mini_rotation_chat.index', ['mini_type' => $slug]));
        $tab->addLink('单图设置', admin_route('mini_single_img.index', ['mini_type' => $slug]));
        return $content->body($tab->withCard());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MiniProgramMainTab(), function (Grid $grid) {
            $typeMaps = MiniProgramMainTab::typeMaps();

            $grid->model()->with(['jump'])->orderByDesc('id');

            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->column('id')->sortable();
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $map[$miniType] ?? $miniType;
            });
            $grid->column('type', '类型')->display(function ($type) use ($typeMaps) {
                return $typeMaps[$type] ?? '';
            });
            $grid->column('icon')->image('', 50);
            $grid->column('name');
            $grid->column('jump.name', '跳转标签名称');
            $grid->column('sort');
            $grid->column('is_show')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) use ($map, $typeMaps) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(2);
                $filter->like('name')->width(2);
                $filter->like('type')->select($typeMaps)->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MiniProgramMainTab(), function (Show $show) {
            $show->field('id');
            $show->field('mini_type');
            $show->field('icon');
            //$show->field('s_icon');
            $show->field('name');
            $show->field('action');
            $show->field('data');
            $show->field('sort');
            $show->field('is_show');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MiniProgramMainTab(), function (Form $form) {
            $form->display('id');
            $form->select('mini_type')->options(function () {
                return getAuthMiniProgramMap(Admin::user()->id);
            })->required();
            $form->select('type')->options(MiniProgramMainTab::typeMaps())->required();
            $form->image('icon')->autoUpload()->uniqueName()->saveFullUrl()->required();
            //$form->image('s_icon')->autoUpload()->uniqueName()->saveFullUrl()->required();
            $form->text('name')->required();
            $form->select('jump_tag_id', '跳转标签')->options(\App\Models\JumpTag::where('is_show', 1)->pluck('name', 'id')->all())->required();
            $form->number('sort')->default(0);
            $form->radio('is_show')->options(['隐藏', '显示'])->default('0');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
